package com.wnxy.admin.service.impl;

import com.wnxy.admin.dto.PermissionMenuVO;
import com.wnxy.admin.entity.Permission;
import com.wnxy.admin.mapper.PermissionMapper;
import com.wnxy.admin.mapper.RolePermissionMapper;
import com.wnxy.admin.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;
    public PermissionServiceImpl(PermissionMapper permissionMapper, RolePermissionMapper rolePermissionMapper){
        this.permissionMapper = permissionMapper;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public List<PermissionMenuVO> findAll() {
        List<Permission> list = permissionMapper.selectList(null);

        List<Permission> finalList = list;
        List<PermissionMenuVO> menuVOS = list.stream().filter(e -> e.getParentId() == 1).map(e ->{
            PermissionMenuVO menuVO = new PermissionMenuVO();
            //2
            List<PermissionMenuVO> children = finalList.stream()
                    .filter(j -> j.getParentId().equals(e.getId())).map(j -> {
                        PermissionMenuVO cmenu = new PermissionMenuVO();
                        cmenu.setId(j.getId());
                        cmenu.setName(j.getPname());
                        cmenu.setUrl(j.getUrl());
                        return cmenu;
                    }).collect(Collectors.toList());

            //设置属性
            menuVO.setId(e.getId());
            menuVO.setName(e.getPname());
            menuVO.setChildren(children);
            return menuVO;
        }).collect(Collectors.toList());
        return menuVOS;
    }
}
